package com.wyz.dao;


import com.wyz.entity.User;

import my.db.QueryHelper;

public class UserDao {
	/**
	 * 增加
	 * */
	public void save(User user) {
		String sql = "insert into user values(null,?,?,?,?,?,?)";
		QueryHelper.update(sql, user.getPassword(),user.getEmail(),user.getPhoneNumber(),user.getUserName(),user.getSex(),user.getFlag());
	}
	
	/**
	 * 修改个人信息表
	 * */
	public void update(User user,String email,String userName) {
		String sql = "update user set email=? , userName=? where id=?";
		QueryHelper.update(sql, email,userName,user.getId());
	}
	
	/**
	 * 修改密码
	 * 
	 * 1
	 * */
	public void changePassword(User user, String password) {
		String sql = "update user set password=? where id=?";
		QueryHelper.update(sql, password,user.getId());
	}
	
	
	
	
	/**
	 * 判定账号密码正确
	 * 
	 * 登陆使用
	 * */
	public int verify(String inputPhoneNumber,String inputPassword) {
		int flag=1;
		User user =new User();
		String sql ="select * from user where phoneNumber=?";
		user=QueryHelper.queryUnique(User.class, sql, inputPhoneNumber);
		if(user==null) {
			flag=2;
		}else {
			if(!user.getPassword().equals(inputPassword)) {
				flag=3;
			}
		}
		return  flag;
	}
	
	public User findByPhoneNumber(String inputPhoneNumber) {
		
		User user =new User();
		String sql ="select * from user where phoneNumber=?";
		user=QueryHelper.queryUnique(User.class, sql, inputPhoneNumber);
		return user;
	}
	
	public User findById(Long id) {
		User user =new User();
		String sql ="select * from user where id=?";
		user=QueryHelper.queryUnique(User.class, sql, id);
		return user;
	}
	
	public void updateFlag(User user,int flag) {
		String sql = "update user set flag=? where id=?";
		QueryHelper.update(sql, flag,user.getId());
	}
	
	public static void main(String[] args) {
		User user=new User();
		 UserDao dao = new UserDao();
//		 dao.save(user);
//		 dao.save(user);
		 System.out.println(dao.verify("123456789", "1234568"));
		 
	}
	
	
}
